package com.itheima.tilas.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tilas.Utils.ResultUtil;
import com.itheima.tilas.dto.SelectStudentByIdDTO;
import com.itheima.tilas.dto.SelectStudentCountClazzNumDTO;
import com.itheima.tilas.dto.SelectStudentCountDegree;
import com.itheima.tilas.dto.SelectStudentPageDTO;
import com.itheima.tilas.mapper.StudentMapper;
import com.itheima.tilas.po.*;
import com.itheima.tilas.service.StudentService;
import com.itheima.tilas.vo.PageVo;
import com.itheima.tilas.vo.SelectStudentCountClazzNumVo;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 学生表服务层实现类
 *
 * @author 132
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    StudentMapper studentMapper;

    @Override
    public ResultUtil selectStudentPage(SelectStudentPageParam param) {
        Page<Object> objects = PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SelectStudentPageDTO> rows = studentMapper.selectStudentPage(param);
        if (CollectionUtils.isEmpty(rows)) {
            return ResultUtil.fail(rows);
        }
        PageVo<SelectStudentPageDTO> pageVo = new PageVo<>();
        pageVo.setTotal(objects.getTotal());
        pageVo.setRows(rows);
        return ResultUtil.success(pageVo);
    }

    @Override
    public int insertStudentOne(InsertStudentParam param) {
        param.setCreateTime(LocalDateTime.now());
        param.setUpdateTime(LocalDateTime.now());
        return studentMapper.insertStudentOne(param);
    }

    @Override
    public int batchDeleteStudentByIds(List<Integer> ids) {
        return studentMapper.batchDeleteStudentByIds(ids);
    }

    @Override
    public SelectStudentByIdDTO selectStudentById(Integer id) {
        return studentMapper.selectStudentById(id);
    }

    @Override
    public int updateStudentById(UpdateStudentByIdParam param) {
        param.setUpdateTime(LocalDateTime.now());
        return studentMapper.updateStudentById(param);
    }

    @Override
    public int updateStudentPunishById(UpdateStudentPunishByIdParam param) {
        param.setUpdateTime(LocalDateTime.now());
        return studentMapper.updateStudentPunishById(param);
    }

    @Override
    public ResultUtil selectStudentCountDegree() {
        List<SelectStudentCountDegree> degrees = studentMapper.selectStudentCountDegree();
        if (CollectionUtils.isNotEmpty(degrees)) {
            return ResultUtil.success("查询成功", degrees);
        } else {
            return ResultUtil.fail("查询失败");
        }
    }

    @Override
    public ResultUtil selectStudentCountClazz() {
        List<SelectStudentCountClazzNumDTO> rows = studentMapper.selectStudentCountClazz();
        if (CollectionUtils.isEmpty(rows)){
            return ResultUtil.fail("本次查询失败");
        }
        SelectStudentCountClazzNumVo clazzNumVo = new SelectStudentCountClazzNumVo();
        rows.forEach(o -> {
            System.out.println(o.getData());
            if (Objects.isNull(o.getData())) {
                o.setData(0);
            }
            clazzNumVo.getClazzList().add(o.getName());
            clazzNumVo.getDataList().add(o.getData());
        });
        return ResultUtil.success("查询成功",clazzNumVo);
    }


}
























